package org.jmc.refmanager.web.wicket.markup.html.form.ChoiceRenderer;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.jmc.refmanager.web.wicket.model.EnumMessageKeyProvider;

public class EnumChoiceRenderer<T extends Enum<T>> implements IChoiceRenderer<T> {
	private static final long serialVersionUID = 1L;

	/** The Component used a the root of the I18N search process */
	private final Component resourceProvider;
 
	private Class<T> enumType;
	
	public EnumChoiceRenderer(final Component resourceProvider, Class<T> t) {
		this.resourceProvider = resourceProvider;
		this.enumType = t;
	}
 
	@Override
	public Object getDisplayValue(final T value) {
		final String key = EnumMessageKeyProvider.getMessageKey(value, enumType);
		return resourceProvider.getString(key);
	}
 
	@Override
	public String getIdValue(final T object, final int index) {
		final Enum<?> enumValue = object;
		return enumValue.name();
	}
}
